﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CuaHangMayTinh.BusinessEntities;
using CuaHangMayTinh.DataProvider;
using CuaHangMayTinh.Utilities;
using System.Data;

namespace CuaHangMayTinh.DataAccessLayer
{
    /// <summary>
    /// Lớp làm việc với csdl để lấy các thông tin từ bảng Info
    /// </summary>
    public class InfoDA
    {
        public InfoDA()
        {

        }

        /// <summary>
        /// Hàm thêm mới Info
        /// </summary>
        /// <param name="entity">InfoEntity muốn thêm mới</param>
        /// <returns></returns>
        public int Insert(InfoEntity entity)
        {
            try
            {
                ParameterBuilder pb = DBFactory.CreateParamBuilder();
                pb.AddParameter("TenDonVi", entity.TenDonVi);
                pb.AddParameter("DiaChi", entity.DiaChi);
                pb.AddParameter("DienThoai", entity.DienThoai);
                pb.AddParameter("Fax", entity.Fax);
                pb.AddParameter("MaSoThue", entity.MaSoThue);
                pb.AddParameter("Email", entity.Email);
                pb.AddParameter("Website", entity.Website);
                pb.AddParameter("MoTa", entity.MoTa);

                return (int)DBFactory.Database.ExecuteNonQuery("Info_Insert", pb.Parameters);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// Hàm cập nhật Info
        /// </summary>
        /// <param name="entity">InfoEntity muốn cập nhật</param>
        /// <returns></returns>
        public bool UpDate(InfoEntity entity)
        {
            try
            {
                ParameterBuilder pb = DBFactory.CreateParamBuilder();

                pb.AddParameter("TenDonVi", entity.TenDonVi);
                pb.AddParameter("DiaChi", entity.DiaChi);
                pb.AddParameter("DienThoai", entity.DienThoai);
                pb.AddParameter("Fax", entity.Fax);
                pb.AddParameter("MaSoThue", entity.MaSoThue);
                pb.AddParameter("Email", entity.Email);
                pb.AddParameter("Website", entity.Website);
                pb.AddParameter("MoTa", entity.MoTa);


                return DBFactory.Database.ExecuteNonQuery("Info_UpDate", pb.Parameters) > 0;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// Hàm lấy thông tin Info
        /// </summary>
        /// <returns></returns>
        public DataTable GetThongTin()
        {
            try
            {
                DataTable dt = new DataTable();
                ParameterBuilder pb = DBFactory.CreateParamBuilder();

                dt = DBFactory.Database.FillDataTable("Info_GetThongTin", pb.Parameters);
                return dt;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// Hàm chuyển thông tin Info từ 1 datatable về 1 list[InfoEntity]
        /// </summary>
        /// <param name="dt">datatable</param>
        /// <returns>list[InfoEntity]</returns>
        public List<InfoEntity> ConvertToList(DataTable dt)
        {
            try
            {
                List<InfoEntity> list = new List<InfoEntity>();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    int id = (int)dt.Rows[i][0];
                    string ten = dt.Rows[i][1].ToString();
                    string diachi = dt.Rows[i][2].ToString();
                    string dienthoai = dt.Rows[i][3].ToString();
                    string fax = dt.Rows[i][4].ToString();
                    string mast = dt.Rows[i][5].ToString();
                    string email = dt.Rows[i][6].ToString();
                    string web = dt.Rows[i][7].ToString();
                    string mota = dt.Rows[i][8].ToString();


                    InfoEntity a = new InfoEntity(id, ten, diachi, dienthoai, fax, mast, email, web, mota);
                    list.Add(a);
                }
                return list;

            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message, ex);
            }
            
        }
    }
}